package clusterConfig

import (
	"context"
	"devops-super/api/configs/v1"
	"devops-super/internal/model/entity"
	"devops-super/internal/service"
	"github.com/gogf/gf/v2/os/gtime"
)

// GetPageLst 分页获取集群配置列表
func (*ControllerV1) GetPageLst(ctx context.Context, req *v1.ClusterConfigGetPageLstReq) (res *v1.ClusterConfigGetPageLstRes, err error) {
	res = &v1.ClusterConfigGetPageLstRes{}
	res.PageLstRes, err = service.ClusterConfig().GetPageLst(ctx, req.PageLstReq)
	return
}

// Add 新增集群配置
func (*ControllerV1) Add(ctx context.Context, req *v1.ClusterConfigAddReq) (res *v1.ClusterConfigAddRes, err error) {
	config := &entity.AppClusterServerConfig{
		ServerKey:   req.ServerKey,
		GroupName:   req.GroupName,
		ConfigKey:   req.ConfigKey,
		ConfigValue: req.ConfigValue,
		Remark:      req.Remark,
		CreatedAt:   gtime.Now(),
		UpdatedAt:   gtime.Now(),
	}
	err = service.ClusterConfig().Add(ctx, config)
	return
}

// Upt 更新集群配置
func (*ControllerV1) Upt(ctx context.Context, req *v1.ClusterConfigUptReq) (res *v1.ClusterConfigUptRes, err error) {
	config := &entity.AppClusterServerConfig{
		Id:          req.Id,
		ServerKey:   req.ServerKey,
		GroupName:   req.GroupName,
		ConfigKey:   req.ConfigKey,
		ConfigValue: req.ConfigValue,
		Remark:      req.Remark,
		UpdatedAt:   gtime.Now(),
	}
	err = service.ClusterConfig().Upt(ctx, config)
	return
}

// Del 删除集群配置
func (*ControllerV1) Del(ctx context.Context, req *v1.ClusterConfigDelReq) (res *v1.ClusterConfigDelRes, err error) {
	err = service.ClusterConfig().Del(ctx, req.Id)
	return
}

// GetOne 获取单条集群配置
func (*ControllerV1) GetOne(ctx context.Context, req *v1.ClusterConfigGetOneReq) (res *v1.ClusterConfigGetOneRes, err error) {
	res1, err := service.ClusterConfig().GetOne(ctx, req.Id)
	if err != nil {
		return
	}
	res = new(v1.ClusterConfigGetOneRes)
	res.Info = res1
	return
}

// GetOptions 获取集群配置选项
func (*ControllerV1) GetOptions(ctx context.Context, req *v1.ClusterConfigGetOptionsReq) (res *v1.ClusterConfigGetOptionsRes, err error) {
	res, err = service.ClusterConfig().GetOptions(ctx, req)
	return
}

// GetGroupConfigs 获取集群配置组
func (*ControllerV1) GetGroupConfigs(ctx context.Context, req *v1.GetClusterGroupConfigsReq) (res *v1.GetClusterGroupConfigsRes, err error) {
	res, err = service.ClusterConfig().GetGroupConfigs(ctx, req)
	return
}

// SetGroupConfigs 保存集群配置组
func (*ControllerV1) SetGroupConfigs(ctx context.Context, req *v1.SetClusterGroupConfigsReq) (res *v1.SetClusterGroupConfigsRes, err error) {
	err = service.ClusterConfig().SetGroupConfigs(ctx, req)
	return
}
